/******************************************************************************
 * Copyright 2023 The Apollo Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

#include "modules/canbus_vehicle/x3epro/protocol/security_brake_command_44.h"
#include "modules/drivers/canbus/common/byte.h"

namespace apollo {
namespace canbus {
namespace x3epro {

using ::apollo::drivers::canbus::Byte;

const int32_t Securitybrakecommand44::ID = 0x44;

// public
Securitybrakecommand44::Securitybrakecommand44() { Reset(); }

uint32_t Securitybrakecommand44::GetPeriod() const {
  // TODO(All) :  modify every protocol's period manually
  static const uint32_t PERIOD = 10 * 1000;
  return PERIOD;
}

void Securitybrakecommand44::Parse(const std::uint8_t* bytes, int32_t length,
                         X3epro* chassis) const {
  chassis->mutable_security_brake_command_44()->set_aeb_leadbrakecmd(aeb_leadbrakecmd(bytes, length));
  chassis->mutable_security_brake_command_44()->set_can_brake_command(can_brake_command(bytes, length));
  chassis->mutable_security_brake_command_44()->set_security_drivercmd_alivecounter(security_drivercmd_alivecounter(bytes, length));
  chassis->mutable_security_brake_command_44()->set_security_drivercmd_checksum(security_drivercmd_checksum(bytes, length));
}

void Securitybrakecommand44::UpdateData(uint8_t* data) {
  set_p_aeb_leadbrakecmd(data, aeb_leadbrakecmd_);
  set_p_can_brake_command(data, can_brake_command_);

  ++security_drivercmd_alivecounter_;
  security_drivercmd_alivecounter_ = (security_drivercmd_alivecounter_) % 16;
  set_p_security_drivercmd_alivecounter(data, security_drivercmd_alivecounter_);

  security_drivercmd_checksum_ =
      data[0] ^ data[1] ^ data[2] ^ data[3] ^ data[4] ^ data[5] ^ data[6];
  set_p_security_drivercmd_checksum(data, security_drivercmd_checksum_);
}

void Securitybrakecommand44::Reset() {
  // TODO(All) :  you should check this manually
  aeb_leadbrakecmd_ = false;
  can_brake_command_ = 0;
  security_drivercmd_alivecounter_ = 0;
  security_drivercmd_checksum_ = 0;
}

Securitybrakecommand44* Securitybrakecommand44::set_aeb_leadbrakecmd(
    bool aeb_leadbrakecmd) {
  aeb_leadbrakecmd_ = aeb_leadbrakecmd;
  return this;
 }

// config detail: {'bit': 0, 'is_signed_var': False, 'len': 1, 'name': 'AEB_LeadBrakeCmd', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': '', 'precision': 1.0, 'type': 'bool'}
void Securitybrakecommand44::set_p_aeb_leadbrakecmd(uint8_t* data,
    bool aeb_leadbrakecmd) {
  int x = aeb_leadbrakecmd;

  Byte to_set(data + 0);
  to_set.set_value(x, 0, 1);
}


Securitybrakecommand44* Securitybrakecommand44::set_can_brake_command(
    int can_brake_command) {
  can_brake_command_ = can_brake_command;
  return this;
 }

// config detail: {'bit': 23, 'is_signed_var': False, 'len': 8, 'name': 'CAN_Brake_Command', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': '', 'precision': 1.0, 'type': 'int'}
void Securitybrakecommand44::set_p_can_brake_command(uint8_t* data,
    int can_brake_command) {
  can_brake_command = ProtocolData::BoundedValue(0, 255, can_brake_command);
  int x = can_brake_command;

  Byte to_set(data + 2);
  to_set.set_value(x, 0, 8);
}


Securitybrakecommand44* Securitybrakecommand44::set_security_drivercmd_alivecounter(
    int security_drivercmd_alivecounter) {
  security_drivercmd_alivecounter_ = security_drivercmd_alivecounter;
  return this;
 }

// config detail: {'bit': 51, 'is_signed_var': False, 'len': 4, 'name': 'Security_DriverCmd_AliveCounter', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': '', 'precision': 1.0, 'type': 'int'}
void Securitybrakecommand44::set_p_security_drivercmd_alivecounter(uint8_t* data,
    int security_drivercmd_alivecounter) {
  security_drivercmd_alivecounter = ProtocolData::BoundedValue(0, 15, security_drivercmd_alivecounter);
  int x = security_drivercmd_alivecounter;

  Byte to_set(data + 6);
  to_set.set_value(x, 0, 4);
}


Securitybrakecommand44* Securitybrakecommand44::set_security_drivercmd_checksum(
    int security_drivercmd_checksum) {
  security_drivercmd_checksum_ = security_drivercmd_checksum;
  return this;
 }

// config detail: {'bit': 63, 'is_signed_var': False, 'len': 8, 'name': 'Security_DriverCmd_CheckSum', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': '', 'precision': 1.0, 'type': 'int'}
void Securitybrakecommand44::set_p_security_drivercmd_checksum(uint8_t* data,
    int security_drivercmd_checksum) {
  security_drivercmd_checksum = ProtocolData::BoundedValue(0, 255, security_drivercmd_checksum);
  int x = security_drivercmd_checksum;

  Byte to_set(data + 7);
  to_set.set_value(x, 0, 8);
}


bool Securitybrakecommand44::aeb_leadbrakecmd(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 0);
  int32_t x = t0.get_byte(0, 1);

  bool ret = x;
  return ret;
}

int Securitybrakecommand44::can_brake_command(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 2);
  int32_t x = t0.get_byte(0, 8);

  int ret = x;
  return ret;
}

int Securitybrakecommand44::security_drivercmd_alivecounter(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 6);
  int32_t x = t0.get_byte(0, 4);

  int ret = x;
  return ret;
}

int Securitybrakecommand44::security_drivercmd_checksum(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 7);
  int32_t x = t0.get_byte(0, 8);

  int ret = x;
  return ret;
}
}  // namespace x3epro
}  // namespace canbus
}  // namespace apollo
